package com.awise.http;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

/**
 * Created by Rover on 17/2/20.
 */

public class RoverSocket {

    public  Socket           socket;
    private DataOutputStream out;
    private DataInputStream in;// 输入流
    private SocketCallback callback;// 信息回调接口

    public RoverSocket(SocketCallback callback){
        this.callback = callback;
    }

    //连接服务器
    public void connect(String ip, int port) {

        try {// 创建一个Socket对象，并指定服务端的IP及端口号
            socket = new Socket();
            SocketAddress address = new InetSocketAddress(ip, port);
            socket.connect(address, 3000);// 连接指定IP和端口
            if (socket.isConnected()) {
                out = new DataOutputStream(socket.getOutputStream());// 获取网络输出流
                in = new DataInputStream(socket.getInputStream());// 获取网络输入流
                callback.connected();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
            //Log.i("链接超时或者什么鬼","");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 返回连接服是否成功
     *
     * @return
     */
    public boolean isConnected() {
        if(socket != null){
            return socket.isConnected();
        }
        return false;
    }

    /**
     * 发送数据
     *
     * @param buffer
     *            信息字节数据
     * @throws IOException
     */
    public void write(byte[] buffer) throws IOException {
        if (out != null) {
            out.write(buffer);
            out.flush();
        }
    }

    /**
     * 断开连接
     *
     * @throws IOException
     */
    public void disconnect() {
        try {
            if (socket != null) {
                if (!socket.isInputShutdown()) {
                    socket.shutdownInput();
                }
                if (!socket.isOutputShutdown()) {
                    socket.shutdownOutput();
                }
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
                socket.close();// 关闭socket
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            callback.disconnect();
            out = null;
            in = null;
            socket = null;// 制空socket对象
        }
    }

    /**
     * 读取网络数据
     *
     * @throws IOException
     */
    public void read() throws IOException {
        if (in != null) {
            byte[] buffer = new byte[124*1];// 缓冲区字节数组，信息不能大于此缓冲区
            byte[] tmpBuffer;// 临时缓冲区
            int len = 0;// 读取长度
            while ((len = in.read(buffer)) > 0) {
                tmpBuffer = new byte[len];// 创建临时缓冲区
                System.arraycopy(buffer, 0, tmpBuffer, 0, len);// 将数据拷贝到临时缓冲区
                callback.receive(tmpBuffer);// 调用回调接口传入得到的数据
                tmpBuffer = null;
            }
        }
    }


}
